﻿using com.foxmail.wyyuan1991.Assignment.Solver.Data;

namespace com.foxmail.wyyuan1991.Assignment.Solver
{
    /// <summary>
    /// 全有全无分配法求解器
    /// </summary>
    public class AoNSolver:IAssignmentAlgorithm
    {
        #region 属性
        /// <summary>
        /// 最短路搜索算法
        /// </summary>
        public IRouteSearchAlgorithm SA { get; set; }//路径搜素器
        #endregion

        #region 方法
        /// <summary>
        /// 全有全无网络分配-不加载流量
        /// </summary>
        /// <param name="net">网络<param>
        /// <param name="f">OD客流分布</param>
        /// <returns></returns>
        public IFlowPattern Solve(INetwork net, IDemand f)
        {
            CostMatrix c = new CostMatrix();
            foreach (IArc a in net.ArcList)
            {
                c[a] = a.PerformaceFunction(0);//a.PerformaceFunction(fd.getFlow(a));
            }
            return Solve(net, f, c);
        }
        /// <summary>
        /// 全有全无网络分配-指定路段阻抗
        /// </summary>
        /// <param name="net">网络</param>
        /// <param name="f">OD客流分布</param>
        /// <param name="c">路段费用矩阵</param>
        /// <returns></returns>
        internal IFlowPattern Solve(INetwork net, IDemand f, CostMatrix c)
        {
            SA.c = c;
            SA.Network = net;

            FlowPattern tempArcLoadDic = new FlowPattern();
            foreach (IDistributionOD odflow in f.ODFlowList)
            {
                IRoute Route = SA.SearchRoute(odflow.OrgNode, odflow.DesNode);
                if (Route == null) continue;
                foreach (IArc a in Route.ArcList)
                {
                    tempArcLoadDic.addFlow(a, odflow.Num);
                }
            }
            return tempArcLoadDic;
        }
        #endregion
    }
}
